﻿using SIE.SCADA.ZemtDemo.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Models;

namespace SIE.SCADA.ZemtDemo.View
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class AddrecordWindow : Window
    {
        private AddrecdViewModel addrecdViewModel;
        public List<scada_jobstep_record> RetryRecordList { get; set; }
        public scada_jobstep_record RetryRecordStep { get; set; }

        public bool RetryProceTag { get; set; }

        public AddrecordWindow(string processName)
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            addrecdViewModel = new AddrecdViewModel(processName);
            this.DataContext = addrecdViewModel;
        }

        /// <summary>
        /// 重打工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RetryProc_Click(object sender, RoutedEventArgs e)
        {
            var result = MessageBox.Show("是否需要重打该工步", "重打确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
            if (result == MessageBoxResult.OK)
            {
                RetryProceTag = addrecdViewModel.GetRetryProce();
                if (!RetryProceTag)
                {
                    MessageBox.Show("工序未完成，不能重打！");
                }
            }
        }

        /// <summary>
        /// 重打工步
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RetryStep_Click(object sender, RoutedEventArgs e)
        {
            if(!addrecdViewModel.GetRetryRecord())
            {
                MessageBox.Show("工序未完成不能重打！");
            }
            if(!addrecdViewModel.Retry_Jobsteprecords.Any())
            {
                MessageBox.Show("没找到补打的工序记录！");
                return;
            }
            if(addrecdViewModel.Retry_Jobsteprecord == null)
            {
                MessageBox.Show("请选中需要补打的工步！");
                return;
            }
            RetryRecordList = addrecdViewModel?.Retry_Jobsteprecords;
            RetryRecordStep = addrecdViewModel?.Retry_Jobsteprecord;
            this.Close();
        }

        private void Quit_Click(object sender, RoutedEventArgs e)
        {
            addrecdViewModel?.Dispose();
            this.Close();
        }

        private void MetroTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Enter:
                    addrecdViewModel?.KeyInput();
                    break;
            }
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            addrecdViewModel?.Clear();
        }
    }
}
